﻿using System;
using System.Collections.Generic;
using System.Linq;
using QQ2564874169.Core.ORM;

namespace Tests.RelationalSql
{
    public class MockSql : Sql
    {
        public object Rs { get; set; }

        protected override void OnDispose()
        {

        }

        protected override int OnExecute(SqlContext context)
        {
            return (int)Rs;
        }

        protected override T OnQueryOne<T>(SqlContext context)
        {
            return (T)Rs;
        }

        protected override IEnumerable<T> OnQuery<T>(SqlContext context)
        {
            return (IEnumerable<T>)Rs;
        }

        protected override IMultipleReader OnMultiple(SqlContext context)
        {
            return new MockMultipleReader(this);
        }

        protected override PageResult<T> OnPager<T>(SqlContext context, PageSet pageset)
        {
            return (PageResult<T>)Rs;
        }

        protected override void OnBeginTransaction()
        {

        }

        protected override void OnCommitTransaction()
        {

        }

        protected override void OnRollbackTransaction()
        {

        }
    }

    public class MockMultipleReader : IMultipleReader
    {
        private int _index;
        private MockSql _sql;

        private object[] Result
        {
            get { return (object[])_sql.Rs; }
        }

        public MockMultipleReader(MockSql sql)
        {
            _sql = sql;
        }

        public void Dispose()
        {
            Disposed?.Invoke(this, new EventArgs());
        }

        public IEnumerable<T> Read<T>()
        {
            return (IEnumerable<T>)Result[_index++];
        }

        public T ReadOne<T>()
        {
            return Read<T>().FirstOrDefault();
        }

        public event EventHandler Disposed;
    }
}
